package com.krio.spaceship;

import org.cocos2d.layers.*;

import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCTextureCache;
import org.cocos2d.particlesystem.CCParticleFire;
import org.cocos2d.particlesystem.CCParticleMeteor;
import org.cocos2d.particlesystem.CCParticleSystem;
import org.cocos2d.particlesystem.CCParticleSystem;
import org.cocos2d.types.CGPoint;

import android.view.MotionEvent;



public class Ship extends CCLayer {
	public float xSpeed;
	public float ySpeed;
	public float yAcceleration;
	public float xAcceleration;
	private CCSprite shipSprite;
	
	public int g = 1;
	
	public Ship(){
		super();
		int shipHeight = 36;
		shipSprite = CCSprite.sprite("ship1.png");
		shipSprite.setAnchorPoint(0.5f, 0.5f);
		shipSprite.setScale(shipHeight / shipSprite.getContentSize().height);
		this.setContentSize(shipSprite.getContentSize().width * shipSprite.getScale(), shipSprite.getContentSize().height * shipSprite.getScale());
		shipSprite.setPosition(this.getContentSize().width / 2, this.getContentSize().height / 2);
		this.addChild(shipSprite);
		this.yAcceleration = -g;
		this.setIsAccelerometerEnabled(true);
		this.setIsTouchEnabled(true);
	}
	
	public void update(float dt){
		float screenWidth = CCDirector.sharedDirector().winSize().width;
		float screenHeight = CCDirector.sharedDirector().winSize().height;
		
		this.ySpeed = this.ySpeed + 60 * dt * this.yAcceleration/8;
		
		float newX = this.getPosition().x + 60 * dt * this.xSpeed / 4;
		float newY = this.getPosition().y + 60 * dt * this.ySpeed / 4;
		
		if(newX < 0){
			newX = 0;
			this.xSpeed = 0;
		}
			
		if(newX > screenWidth - this.getContentSize().width)
		{
			newX = screenWidth - this.getContentSize().width;
			this.xSpeed = 0;
		}
			
		if(newY < 0)
		{
			newY = 0;
			this.yAcceleration = 0;
			this.ySpeed = 0;
		}
		if(newY > screenHeight - this.getContentSize().height)
		{
			newY = screenHeight - this.getContentSize().height;
			this.ySpeed = 0;
		}
		this.setPosition(newX, newY);
		this.updateAngle( 10 * 60 * dt * this.xAcceleration);
	}
	
	private void updateAngle(float x){
		this.shipSprite.setRotation((float) (x));
	}
	
	public void turnOnUpEngine()
	{
		this.yAcceleration = 2;
	}
	
	public void turnOffUpEngine()
	{
		this.yAcceleration = -g;
	}
	
	public void ccAccelerometerChanged(float x, float y, float z){
		float maximumSpeed = 10;
		float screenWidth = CCDirector.sharedDirector().winSize().width;
		float screenHeight = CCDirector.sharedDirector().winSize().height;
		
		
		if(this.getPosition().x < 0 || this.getPosition().y + this.getContentSize().height * (1 - this.getAnchorPoint().y) > screenHeight
				|| this.getPosition().y < 0 || this.getPosition().x + this.getContentSize().width * (1 - this.getAnchorPoint().x) > screenWidth)
			return;
		
		this.xAcceleration = y/4;
		if(Math.abs(this.xSpeed + this.xAcceleration) < maximumSpeed ){
			this.xSpeed += this.xAcceleration;
		}
		
	}
	
	public boolean ccTouchesBegan(MotionEvent event){
//        CGPoint convertedLocation = CCDirector.sharedDirector()
//            	.convertToGL(CGPoint.make(event.getX(), event.getY()));
//        
//        
        this.turnOnUpEngine();
        
        return true;
	}
	
	public boolean ccTouchesMoved(MotionEvent event){
		// do nothing
		
		return true;
	}
	
	public boolean ccTouchesEnded(MotionEvent event){
		this.turnOffUpEngine();
		return false;
	}
}
